package br.usp.ime.origami.model;

import java.awt.Polygon;
import java.util.Arrays;
import java.util.List;

public class Triangle implements Polygonable {

	private SimplePoint p1, p2, p3;

	public Triangle(SimplePoint p1, SimplePoint p2, SimplePoint p3) {
		this.p1 = p1;
		this.p2 = p2;
		this.p3 = p3;
	}

	public SimplePoint getP1() {
		return p1;
	}

	public SimplePoint getP2() {
		return p2;
	}

	public SimplePoint getP3() {
		return p3;
	}

	public List<SimplePoint> getPoints() {
		return Arrays.asList(new SimplePoint[] { p1, p2, p3 });
	}

	public Polygon toPolygon() {
		Polygon p = new Polygon();
		
		// inverting p2 and p3 for CCW in java 
		p.addPoint((int) p1.getX(), (int) p1.getY());
		p.addPoint((int) p3.getX(), (int) p3.getY());
		p.addPoint((int) p2.getX(), (int) p2.getY());

		return p;
	}

	@Override
	public String toString() {
		return String.format("[Triangle %s %s %s]", p1, p2, p3);
	}

	@Override
	public boolean equals(Object obj) {
		Triangle t = (Triangle) obj;

		return (p1.equals(t.p1) && p2.equals(t.p2) && p3.equals(t.p3))
				|| (p1.equals(t.p2) && p2.equals(t.p3) && p3.equals(t.p1))
				|| (p1.equals(t.p3) && p2.equals(t.p1) && p3.equals(t.p2));
	}

	@Override
	public int hashCode() {
		return p1.hashCode() * p2.hashCode() * p3.hashCode();
	}

}
